/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test.controllers;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import test.controllers.exceptions.NonexistentEntityException;
import test.controllers.exceptions.PreexistingEntityException;
import test.entities.Portefeuille;
import test.entities.Action;
import test.entities.PortefeuilleAction;
import test.entities.PortefeuilleActionPK;

/**
 *
 * @author Issam
 */
public class PortefeuilleActionJpaController implements Serializable {

    public PortefeuilleActionJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(PortefeuilleAction portefeuilleAction) throws PreexistingEntityException, Exception {
        if (portefeuilleAction.getPortefeuilleActionPK() == null) {
            portefeuilleAction.setPortefeuilleActionPK(new PortefeuilleActionPK());
        }
        portefeuilleAction.getPortefeuilleActionPK().setNumAction(portefeuilleAction.getAction().getNumAction());
        portefeuilleAction.getPortefeuilleActionPK().setNumPort(portefeuilleAction.getPortefeuille().getNumPort());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Portefeuille portefeuille = portefeuilleAction.getPortefeuille();
            if (portefeuille != null) {
                portefeuille = em.getReference(portefeuille.getClass(), portefeuille.getNumPort());
                portefeuilleAction.setPortefeuille(portefeuille);
            }
            Action action = portefeuilleAction.getAction();
            if (action != null) {
                action = em.getReference(action.getClass(), action.getNumAction());
                portefeuilleAction.setAction(action);
            }
            em.persist(portefeuilleAction);
            if (portefeuille != null) {
                portefeuille.getPortefeuilleActionCollection().add(portefeuilleAction);
                portefeuille = em.merge(portefeuille);
            }
            if (action != null) {
                action.getPortefeuilleActionCollection().add(portefeuilleAction);
                action = em.merge(action);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findPortefeuilleAction(portefeuilleAction.getPortefeuilleActionPK()) != null) {
                throw new PreexistingEntityException("PortefeuilleAction " + portefeuilleAction + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(PortefeuilleAction portefeuilleAction) throws NonexistentEntityException, Exception {
        portefeuilleAction.getPortefeuilleActionPK().setNumAction(portefeuilleAction.getAction().getNumAction());
        portefeuilleAction.getPortefeuilleActionPK().setNumPort(portefeuilleAction.getPortefeuille().getNumPort());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            PortefeuilleAction persistentPortefeuilleAction = em.find(PortefeuilleAction.class, portefeuilleAction.getPortefeuilleActionPK());
            Portefeuille portefeuilleOld = persistentPortefeuilleAction.getPortefeuille();
            Portefeuille portefeuilleNew = portefeuilleAction.getPortefeuille();
            Action actionOld = persistentPortefeuilleAction.getAction();
            Action actionNew = portefeuilleAction.getAction();
            if (portefeuilleNew != null) {
                portefeuilleNew = em.getReference(portefeuilleNew.getClass(), portefeuilleNew.getNumPort());
                portefeuilleAction.setPortefeuille(portefeuilleNew);
            }
            if (actionNew != null) {
                actionNew = em.getReference(actionNew.getClass(), actionNew.getNumAction());
                portefeuilleAction.setAction(actionNew);
            }
            portefeuilleAction = em.merge(portefeuilleAction);
            if (portefeuilleOld != null && !portefeuilleOld.equals(portefeuilleNew)) {
                portefeuilleOld.getPortefeuilleActionCollection().remove(portefeuilleAction);
                portefeuilleOld = em.merge(portefeuilleOld);
            }
            if (portefeuilleNew != null && !portefeuilleNew.equals(portefeuilleOld)) {
                portefeuilleNew.getPortefeuilleActionCollection().add(portefeuilleAction);
                portefeuilleNew = em.merge(portefeuilleNew);
            }
            if (actionOld != null && !actionOld.equals(actionNew)) {
                actionOld.getPortefeuilleActionCollection().remove(portefeuilleAction);
                actionOld = em.merge(actionOld);
            }
            if (actionNew != null && !actionNew.equals(actionOld)) {
                actionNew.getPortefeuilleActionCollection().add(portefeuilleAction);
                actionNew = em.merge(actionNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                PortefeuilleActionPK id = portefeuilleAction.getPortefeuilleActionPK();
                if (findPortefeuilleAction(id) == null) {
                    throw new NonexistentEntityException("The portefeuilleAction with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(PortefeuilleActionPK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            PortefeuilleAction portefeuilleAction;
            try {
                portefeuilleAction = em.getReference(PortefeuilleAction.class, id);
                portefeuilleAction.getPortefeuilleActionPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The portefeuilleAction with id " + id + " no longer exists.", enfe);
            }
            Portefeuille portefeuille = portefeuilleAction.getPortefeuille();
            if (portefeuille != null) {
                portefeuille.getPortefeuilleActionCollection().remove(portefeuilleAction);
                portefeuille = em.merge(portefeuille);
            }
            Action action = portefeuilleAction.getAction();
            if (action != null) {
                action.getPortefeuilleActionCollection().remove(portefeuilleAction);
                action = em.merge(action);
            }
            em.remove(portefeuilleAction);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<PortefeuilleAction> findPortefeuilleActionEntities() {
        return findPortefeuilleActionEntities(true, -1, -1);
    }

    public List<PortefeuilleAction> findPortefeuilleActionEntities(int maxResults, int firstResult) {
        return findPortefeuilleActionEntities(false, maxResults, firstResult);
    }

    private List<PortefeuilleAction> findPortefeuilleActionEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(PortefeuilleAction.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public PortefeuilleAction findPortefeuilleAction(PortefeuilleActionPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(PortefeuilleAction.class, id);
        } finally {
            em.close();
        }
    }

    public int getPortefeuilleActionCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<PortefeuilleAction> rt = cq.from(PortefeuilleAction.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
